package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.DateUtils;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkRejectReasonMapper;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.ResponseDTO;
import com.jzo2o.customer.service.WorkRejectReasonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 机构下属服务人员 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-07-18
 */
@Service
@Slf4j
public class WorkRejectReasonServiceImpl extends ServiceImpl<WorkRejectReasonMapper, WorkerCertification> implements WorkRejectReasonService {
        @Resource
        private WorkerCertificationAuditMapper  workerCertificationAuditMapper;


    /**
     * 服务认证申请
     *
     * @param workerCertificationAuditAddReqDTO
     */
    @Override
    public void certificationAudit(WorkerCertificationAuditAddReqDTO workerCertificationAuditAddReqDTO) {

        // 获取用户ID
        Long userId = UserContext.currentUserId();
        if (userId == null) {
            throw new BadRequestException("用户ID为空");
        }

        log.info("用户ID: {}", userId);

        // 属性转换
        WorkerCertification certification = BeanUtils.toBean(workerCertificationAuditAddReqDTO, WorkerCertification.class);

        // 查询当前用户的审核记录
        QueryWrapper<WorkerCertificationAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("serve_provider_id", userId);
        WorkerCertificationAudit existingAudit = workerCertificationAuditMapper.selectOne(queryWrapper);

        log.info("查询到的现有审核记录: {}", existingAudit);

        if (existingAudit == null) {
            // 如果没有现有的审核记录，则新增一条
            WorkerCertificationAudit workerCertificationAudit = BeanUtils.toBean(workerCertificationAuditAddReqDTO, WorkerCertificationAudit.class);

            workerCertificationAudit.setId(userId);
            workerCertificationAudit.setAuditStatus(CertificationStatusEnum.INIT.getStatus());
            workerCertificationAudit.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            workerCertificationAudit.setServeProviderId(userId);
            workerCertificationAudit.setCreateTime(DateUtils.now());

            // 保存审计记录
            workerCertificationAuditMapper.insert(workerCertificationAudit);

            // 更新WorkerCertification对象
            certification.setId(userId);
            certification.setCertificationTime(DateUtils.now());
            certification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());

            // 保存certification对象
            baseMapper.insert(certification);

            log.info("初始化状态处理完成");
        } else if (CertificationStatusEnum.FAIL.getStatus() == existingAudit.getCertificationStatus()) {
            // 如果有现有的审核记录且状态为驳回，则删除旧的审核记录和认证记录
            workerCertificationAuditMapper.deleteById(existingAudit.getId());
            baseMapper.deleteById(userId);

            // 更新新数据
            WorkerCertificationAudit workerCertificationAudit = BeanUtils.toBean(workerCertificationAuditAddReqDTO, WorkerCertificationAudit.class);

            workerCertificationAudit.setId(userId);
            workerCertificationAudit.setAuditStatus(CertificationStatusEnum.INIT.getStatus());
            workerCertificationAudit.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            workerCertificationAudit.setServeProviderId(userId);
            workerCertificationAudit.setCreateTime(DateUtils.now());

            // 保存新的审计记录
            workerCertificationAuditMapper.insert(workerCertificationAudit);

            // 更新WorkerCertification对象
            certification.setId(userId) ;
            certification.setCertificationTime(DateUtils.now());
            certification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());

            // 保存certification对象
            baseMapper.insert(certification);

            log.info("驳回状态处理完成");
        } else {
            throw new BadRequestException("不支持的状态");
        }
//
//        if (ObjectUtil.isEmpty(certification)) {
//            throw new BadRequestException("参数不能为空");
//        }
//        WorkerCertificationAudit workerCertificationAudit;
//        //如果状态为初始化则新增
//        if ( CertificationStatusEnum.INIT.getStatus() == certification.getCertificationStatus()) {
//// 创建WorkerCertification对象
//            workerCertificationAudit = BeanUtils.toBean(workerCertificationAuditAddReqDTO, WorkerCertificationAudit.class);
//
//// 创建WorkerCertificationAudit对象
//            workerCertificationAudit.setId(userId);
//            workerCertificationAudit.setAuditStatus(CertificationStatusEnum.INIT.getStatus());
//            workerCertificationAudit.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
//            workerCertificationAudit.setServeProviderId(UserContext.currentUserId());
//            workerCertificationAudit.setCreateTime(DateUtils.now());
//
//// 保存审计记录
//            workerCertificationAuditMapper.insert(workerCertificationAudit);
//
//
//// 更新WorkerCertification对象
//            certification.setId(userId);
//            certification.setCertificationTime(DateUtils.now());
//            certification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
//            certification.setId(UserContext.currentUserId());
//            // 保存certification对象
//            baseMapper.insert(certification);
//            //如果状态为驳回
//        } else ( CertificationStatusEnum.INIT.getStatus() == certification.getCertificationStatus()) {
//        }
//
//        //如果审核失败 需要重新提交申请 并且删除旧数据 更新 新数据
//        QueryWrapper<WorkerCertificationAudit> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("serve_provider_id",userId);
//        WorkerCertificationAudit workerCertificationAudit1 = workerCertificationAuditMapper.selectOne(queryWrapper);
//        if (ObjectUtil.isNotEmpty(workerCertificationAudit1)){
//            //删除旧数据
//            workerCertificationAuditMapper.deleteById(workerCertificationAudit1.getId());
//            baseMapper.deleteById(userId);
//            //更新新数据
//            baseMapper.insert(certification);
//            workerCertificationAuditMapper.insert(workerCertificationAudit);
//        }



    }

    /**
     * 查询最新的驳回原因
     */
    @Override
    public ResponseDTO rejectReason() {

        //获取用户id

        Long serveProviderId = UserContext.currentUserId();
        WorkerCertificationAudit certificationAudit = workerCertificationAuditMapper.selectById(serveProviderId);


        String rejectReason = certificationAudit.getRejectReason();
        if (rejectReason==null){
            throw new BadRequestException("无驳回原因");
        }
        //返回
        ResponseDTO responseDTO = new ResponseDTO();
        responseDTO.setRejectReason(rejectReason);
        return responseDTO;


    }

}
